//
//  SBBaseTextView.m
//  SBAdaptiveTool
//
//  Created by user on 2018/9/10.
//  Copyright © 2018年 陈振旭. All rights reserved.
//

#import "SBBaseTextView.h"

#import "AdaptiveHeader.h"

@implementation SBBaseTextView

-(void)awakeFromNib {
    [super awakeFromNib];
    
    [self setFont:[UIFont fontWithDescriptor:[self.font fontDescriptor] size:SBScaleNumber(self.font.pointSize)]];
    
}

- (BOOL)becomeFirstResponder {
    BOOL become = [super becomeFirstResponder];
    
    // 成为第一响应者时注册通知监听文本变化
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange:) name:UITextViewTextDidChangeNotification object:nil];
    
    return become;
}

- (BOOL)resignFirstResponder {
    BOOL resign = [super resignFirstResponder];
    
    // 注销第一响应者时移除文本变化的通知, 以免影响其它的`UITextView`对象.
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidChangeNotification object:nil];
    
    return resign;
}


- (void)textDidChange:(NSNotification *)note {
    // 会重新调用drawRect:方法
    [self setNeedsDisplay];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


/**
 * 每次调用drawRect:方法，都会将以前画的东西清除掉
 */
- (void)drawRect:(CGRect)rect {
    
    if (self.hasText) return;
    
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = self.font;
    attrs[NSForegroundColorAttributeName] = self.placeholderColor?self.placeholderColor:SBRGBA(199, 199, 205, 0.7);
    
    rect.origin.x = 5;
    rect.origin.y = 8;
    rect.size.width -= 2 * rect.origin.x;
    [self.placeholder drawInRect:rect withAttributes:attrs];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self setNeedsDisplay];
}


#pragma mark - setter
- (void)setPlaceholder:(NSString *)placeholder
{
    _placeholder = [placeholder copy];
    
    [self setNeedsDisplay];
}

- (void)setPlaceholderColor:(UIColor *)placeholderColor {
    _placeholderColor = placeholderColor;
    
    [self setNeedsDisplay];
}

- (void)setText:(NSString *)text {
    [super setText:text];
    
    [self setNeedsDisplay];
}

- (void)setAttributedText:(NSAttributedString *)attributedText {
    [super setAttributedText:attributedText];
    
    [self setNeedsDisplay];
}

- (void)setSBBorderColor:(UIColor *)SBBorderColor {
    if (!SBBorderColor) {
        return;
    }
    _SBMasksToBounds = SBBorderColor;
    self.layer.borderColor = SBBorderColor.CGColor;
    [self setNeedsDisplay];
}

- (void)setSBCornerRadius:(CGFloat)SBCornerRadius {
    if (!SBCornerRadius) {
        return;
    }
    _SBCornerRadius = SBCornerRadius;
    self.layer.cornerRadius = SBScaleNumber(SBCornerRadius);
    [self setNeedsDisplay];
}

- (void)setSBBorderWidth:(CGFloat)SBBorderWidth {
    if (!SBBorderWidth) {
        return;
    }
    _SBBorderWidth = SBBorderWidth;
    self.layer.borderWidth = SBScaleNumber(SBBorderWidth);
    [self setNeedsDisplay];
}

- (void)setSBMasksToBounds:(BOOL)SBMasksToBounds {
    _SBMasksToBounds = SBMasksToBounds;
    self.layer.masksToBounds = SBMasksToBounds;
    [self setNeedsDisplay];
}

@end
